home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Mac-Source 1994 July
/
Mac-Source_July_1994.iso
/
Apple Script
/
OSAX
/
Return Record OSAX
/
GetFontInfo2.c
< prev
next >
Wrap
C/C++ Source or Header
|
1993-05-07
|
3KB
|
123 lines
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// GetFontInfo2.c
// Written by Donald Olson
// Copyright ®1993 Apple Computer Inc.
// All rights reserved.
//
// This is a simple Scripting Addition that returns the FontInfo of the
// specified font and size. If the size parameter is not specified, default
// size of 12 is used. Written for a WWDC presentation by Donald Olson
// and Donn Denman.
//
// Syntax: fontinfo <font name> [size <short>]
// Returns: record {leading:short;ascent:short;descent:short; max width: short}
//
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Our header
Boolean GetFontNumber( Str255 fontName, short *fontNum );
// Our includes
#include <string.h>
#include <Quickdraw.h>
#include <Fonts.h>
#include <Errors.h>
#include <AppleEvents.h>
/*struct FontInfo {
short ascent;
short descent;
short widMax;
short leading;
};
*/
// These are the constants defined for the properties we defined to
// get AppleScript to return a record with our font info in it.
#define kLeading 'lead'
#define kAscent 'sent'
#define kDescent 'desc'
#define kMaxWidth 'mxwd'
//AEVTOLIEfinf
#define keyFONTSIZE 'FSIZ'
pascal OSErr main(AppleEvent *theEvent,
AppleEvent *theReply,
long theRefCon)
{
OSErr theErr = noErr;
GrafPort fontPort;
GrafPtr savedPort, fontPortPtr = &fontPort;
Str255 fontName;
long actualSize;
short fontSize, fontNum;
DescType typeCode;
FontInfo fInfo;
AERecord ourRecord = {typeNull, nil};
theErr = AEGetParamPtr(theEvent, keyDirectObject,
typeChar, &typeCode, (Ptr)&fontName,
sizeof(fontName), &actualSize);
if(theErr != noErr)
return theErr;
fontName[actualSize] = '\0'; // c string please
c2pstr((char*)fontName); // and now pascal
theErr = AEGetParamPtr(theEvent, keyFONTSIZE,
typeShortInteger, &typeCode, (Ptr)&fontSize,
sizeof(fontSize), &actualSize);
if(theErr != noErr)
fontSize = 12;
if(!GetFontNumber(fontName, &fontNum))
return fontDecError;
GetPort(&savedPort);
OpenPort(&fontPortPtr);
SetPort(&fontPortPtr);
TextFont(fontNum);
TextSize(fontSize);
GetFontInfo(&fInfo);
SetPort(&savedPort);
theErr = AECreateList(nil, 0, true, &ourRecord);
if(theErr != noErr) return theErr;
theErr = AEPutKeyPtr(&ourRecord, kLeading, typeShortInteger, (Ptr)&fInfo.leading,
sizeof(fInfo.leading));
theErr = AEPutKeyPtr(&ourRecord, kAscent, typeShortInteger, (Ptr)&fInfo.ascent,
sizeof(fInfo.ascent));
theErr = AEPutKeyPtr(&ourRecord, kDescent, typeShortInteger, (Ptr)&fInfo.descent,
sizeof(fInfo.descent));
theErr = AEPutKeyPtr(&ourRecord, kMaxWidth, typeShortInteger, (Ptr)&fInfo.widMax,
sizeof(fInfo.widMax));
theErr = AEPutParamDesc(theReply, keyDirectObject, &ourRecord);
return theErr;
}
Boolean GetFontNumber( Str255 fontName, short *fontNum )
{
Str255 systemFontName;
GetFNum( fontName, fontNum );
if ( *fontNum == 0) {
/* Either we didn't find the font, or we were looking for the system
* font. */
GetFontName( 0, systemFontName );
return EqualString( fontName, systemFontName, FALSE, FALSE );
}
else
return TRUE;
}